package net.ewon.avatar.pkgmgmt;

import static org.junit.Assert.assertEquals;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import javax.annotation.Resource;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageFile;
import net.ewon.avatar.common.pkg.AvatarPackageWithFile;
import net.ewon.avatar.common.pkg.Version;
import net.ewon.avatar.common.util.AvatarPackageFileVisitor;
import net.ewon.avatar.common.util.FilePathUtils;

import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.After;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * To run this integration test:
 * 
 * 1. Make sure the mongodb is running
 * 2. Make sure AvatarPackageMgmtService is running
 * 
 * mvn integration-test -Dpackage.name=NaviConfig -Dpackage.version=1.0.0 -Dpackage.location=e:\Avatar\NaviConfig\target\NaviConfig\
 * or
 * mvn  integration-test -Dpackage.name=NaviConfig -Dpackage.version=1.0.0 -Dpackage.location=/Users/ewon/Documents/work/NaviConfig/target/NaviConfig
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:AvatarPackageMgmtService-test.xml")
@Category({net.ewon.test.LinuxTests.class,net.ewon.test.WindowsTests.class}) 
public class AvatarPackageMgmtServiceIT {
	 
	@Resource
	private WebClient client;
	
	private AvatarPackage expectedAvatarPackage;
	
	@After
	public void tearDown() {
		client.reset();
		client.path("AvatarPackageMgmtService/removeAvatarPackage");
		client.type("application/json").accept("application/json");
		if(expectedAvatarPackage!=null) {		
			client.post(expectedAvatarPackage);
		}
	}
	
	

	@Test
	public void test() throws Exception{
		String location = System.getProperty("package.location");
		String pkgName = System.getProperty("package.name");
		String version = System.getProperty("package.version");

		Path packagePath = Paths.get(location);
		AvatarPackageFileVisitor visitor = new AvatarPackageFileVisitor(
				packagePath);
		Files.walkFileTree(packagePath, visitor);

		List<AvatarPackageFile> files = visitor.getAvatarPackageFiles();
		fillAvatarPackageFilesName(pkgName, version, files);

		AvatarPackage avatarPackage = new AvatarPackage(pkgName,
				Version.parse(version), null);

		AvatarPackageWithFile pair =
				new AvatarPackageWithFile(avatarPackage, files);

		// client.path("AvatarPackageMgmtService/getAvatarPackage");
		client.path("AvatarPackageMgmtService/uploadAvatarPackageWithFiles");
		client.type("application/json").accept("application/json");

		String result = client.post(pair, String.class);
		// System.out.println("result=" + result);
		client.reset();
		client.type("application/json").accept("application/json");
		
		client.path("AvatarPackageMgmtService/getAvatarPackage");
		client.query("pkgName", pkgName);
		client.query("version", version);
		
		
		expectedAvatarPackage = client.get(AvatarPackage.class);
		
		assertEquals(pkgName, expectedAvatarPackage.getName());
		assertEquals(version, expectedAvatarPackage.getVersion().toString());
		
		for (String fileId : expectedAvatarPackage.getFileIds()) {

			client.reset();
			client.resetQuery();
			client.type("application/json").accept("application/json");
			
			client.path("AvatarPackageMgmtService/getAvatarPackageFile");

			client.query("fileId", fileId);
			AvatarPackageFile existingFile = client
					.get(AvatarPackageFile.class);
			System.out.println("existingFile.name=" + existingFile.getName() + ",id=" + existingFile.getId()
					+ ",size=" + existingFile.getData().length); 
		} 

	}
	
	private void fillAvatarPackageFilesName(String pkgName, String version,
			List<AvatarPackageFile> avatarPackageFiles) {
		for (AvatarPackageFile file : avatarPackageFiles) {
			file.setName(FilePathUtils.normalizeLinux(pkgName + "/" + version
					+ "/" + file.getName()));
		}
	}

}
